package com.woniuxy.user.controller;

import com.woniuxy.commons.request.ResPage;
import com.woniuxy.commons.request.ResponseResult;
import com.woniuxy.commons.util.JWTUtil;
import com.woniuxy.user.dto.EmployeeDto;
import com.woniuxy.user.dto.UserInfoDto;
import com.woniuxy.user.service.PermsService;
import com.woniuxy.user.service.UserService;
import com.woniuxy.user.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: Mr·Xiang
 * @create 2024-04-03 11:45
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    //测试1
    @Resource
    private UserService userService;

    @Resource
    private PermsService permsService;

    @GetMapping("findById/{id}")
    public ResponseResult<UserInfoDto> findById(@PathVariable("id") Integer id){
        log.info("findById:{}",id);
        return new ResponseResult<>(ResponseResult.OK, userService.findById(id));
    }

    /**
     * 通过token查询用户的权限信息
     * @param token
     * @return
     */
    @GetMapping("/findPermsByToken/{token}")
    public ResponseResult<List<String>> findPermsByToken(@PathVariable("token") String token){
        return new ResponseResult<>(ResponseResult.OK, permsService.findByUid(JWTUtil.getuid(token)));
    }

    @GetMapping("/findBorrowerAndGuarantee")
    public ResponseResult<BGVo> findBorrowerAndGuarantee(){
        return new ResponseResult<>(ResponseResult.OK, userService.findBorrowerAndGuarantee());
    }

    /**
     * 修改密码前对旧密码进行校验
     */
    @PostMapping("/checkPass")
    public ResponseResult<Boolean> checkPass(@RequestBody LoginVo loginVo){
        return new ResponseResult<>(ResponseResult.OK, userService.checkPass(loginVo));
    }
    /**
     * 修改密码
     */
    @PostMapping("/updatePassword")
    public ResponseResult<Boolean> updatePassword(@RequestBody LoginVo loginVo){
        return new ResponseResult<>(ResponseResult.OK, userService.updatePassword(loginVo));
    }
    /**
     * 获取员工列表
     */
    @GetMapping("/getEmployeeList")
    public ResponseResult<ResPage<EmployeeDto>> getEmployeeList(EmployeeVo employeeVo){
        return new ResponseResult<>(ResponseResult.OK, userService.getEmployeeList(employeeVo));
    }
    /**
     * 添加员工
     */
    @PostMapping("/addEmployee")
    public ResponseResult<Boolean> addEmployee(@RequestBody AddEmployeeVo addEmployeeVo){
        return new ResponseResult<>(ResponseResult.OK, userService.addEmployee(addEmployeeVo));
    }
    /**
     * 修改员工信息
     */
    @PutMapping("/editEmployee")
    public ResponseResult<Boolean> editEmployee(@RequestBody AddEmployeeVo addEmployeeVo){
        return new ResponseResult<>(ResponseResult.OK, userService.editEmployee(addEmployeeVo));
    }
    /**
     * 校验用户名是否可用
     */
    @GetMapping("/checkUname/{uname}")
    public ResponseResult<Boolean> checkUname(@PathVariable("uname") String uname){
        return new ResponseResult<>(ResponseResult.OK, userService.checkUname(uname));
    }
    /**
     * 锁定账户
     */
    @PutMapping("/enableAccount/{id}")
    public ResponseResult<Boolean> enableAccount(@PathVariable("id") Integer id){
        return new ResponseResult<>(ResponseResult.OK, userService.enableAccount(id));
    }
    /**
     * 解锁账户
     */
    @PutMapping("/disableAccount/{id}")
    public ResponseResult<Boolean> disableAccount(@PathVariable("id") Integer id){
        return new ResponseResult<>(ResponseResult.OK, userService.disableAccount(id));
    }
    /**
     * 删除员工
     */
    @DeleteMapping("/deleteEmployee/{id}/{did}")
    public ResponseResult<Boolean> deleteEmployee(@PathVariable("id") Integer id,@PathVariable("did") Integer did){
        return new ResponseResult<>(ResponseResult.OK, userService.deleteEmployee(id,did));
    }
    /**
     * 注册用户
     */
    @PostMapping("/register")
    public ResponseResult<Boolean> registerConsumer(@RequestBody RegisterVo registerVo){
        return new ResponseResult<>(ResponseResult.OK, userService.registerConsumer(registerVo));
    }
}
